package com.pb.metadata.service;

import com.pb.infra.auth.mapper.CommonMapper;
import com.pb.infra.oth.util.DateTimeUtil;
import com.pb.metadata.mapper.CalendarMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liaohaoyuan
 * 2022.4.3
 */
@Service
public class CalendarService {

    @Resource
    private CalendarMapper calendarMapper;

    @Resource
    private CommonMapper commonMapper;

    /**
     * 查询假期数据
     * @param paramMap
     * @return
     */
    public List<Map<String,Object>> getHoliday(Map<String,Object> paramMap){
        return calendarMapper.getHoliday(paramMap);
    }

    /**
     * 手动设置假期数据
     * @param paramMap
     * @return
     */
    public void saveHoliday(Map<String,Object> paramMap) {
        try{
            String time = (String) paramMap.get("time");
            String holidayFlag = (String) paramMap.get("holidayFlag");
            if (holidayFlag.equals("1")){
                Integer id= calendarMapper.selectHoliday(paramMap);
                if (id == 0){
                    DateFormat df = new SimpleDateFormat("yyyyMMdd");
                    Date parse = df.parse(time);
                    paramMap.put("date",parse);
                    calendarMapper.saveHoliday(paramMap);
                }
            }else {
                calendarMapper.delteHoliday(paramMap);
            }
        }catch (ParseException e){
            e.printStackTrace();
        }
    }

    /**
     * 初始化年份假期数据
     * @param paramMap
     * @return
     */
    @Transactional
    public String initYearsData(Map<String,Object> paramMap){
        //得到年份
        String initYear =(String) paramMap.get("initYear");
        List<String> yearWeek = DateTimeUtil.getYearWeek(initYear);
        Map<String,Object> map=calendarMapper.getTableName(paramMap);
        map.put("initYear",initYear);
        map.put("flag",null);
        List<Map<String,Object>> notData = calendarMapper.getTypeData(map);//需要插入的国家类型
        StringBuilder sbf = new StringBuilder();
            /*if (notData.size()>0){
                sbf.append("初始化");
            }*/
        for (int i = 0; i < notData.size(); i++) {
            paramMap.clear();
            paramMap.put("startDate",initYear+"0101");
            paramMap.put("endDate",initYear+"1231");
            paramMap.put("type",notData.get(i).get("code"));
            paramMap.put("initYear",initYear);
            calendarMapper.savaHolidayInitSigns(paramMap);
            calendarMapper.initCapWorkDate(notData.get(i).get("code").toString() ,yearWeek);
        }
        if(notData.size()==0){
            sbf.append("本次未进行初始化，因初始化不能重复进行，每年只能初始化一次。");
        }else{
            sbf.append("初始化成功");
        }
        return sbf.toString();
    }

    /**
     * 获取初始化节日类型清单
     * @param paramMap
     * @return
     */
    public List<Map<String,Object>> getTableData(Map<String,Object> paramMap){
        Map<String, Object> map = calendarMapper.getTableName(paramMap);
        map.put("initYear",paramMap.get("initYear"));
        return calendarMapper.getTableData(map);
    }

    /**
     * 获取码值
     * @param paramMap
     * @return
     */
    public List<Map<String,Object>> getCodeTypeData(Map<String,Object> paramMap){
        Map<String,Object> map=calendarMapper.getTableName(paramMap);
        List<Map<String,Object>> list = calendarMapper.getCodeList(map);
        return list;
    }

    /**
     * 获取码值
     * @param paramMap
     * @return
     */
    public List<Map<String,Object>> getCodeTypeData2(Map<String,Object> paramMap){
        Map<String,Object> map=calendarMapper.getTableName(paramMap);
        List<Map<String,Object>> list = calendarMapper.getCodeList2(map);
        return list;
    }

}
